package controller.ur;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import model.dao.PhotoDaoLocal;
import model.dao.TagDaoLocal;
import model.entity.Photo;
import model.entity.Tag;
import controller.CUtil;

/**
 * Servlet implementation class SPhotoRemoveTag
 */
public class SPhotoRemoveTag extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	@EJB
	private PhotoDaoLocal photoDao;
	
	@EJB
	private TagDaoLocal tagDao;
	
	private Logger log=Logger.getLogger(SPhotoRemoveTag.class);
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SPhotoRemoveTag() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!CUtil.checkLogIn(request, response))
			return;
		
		int photoId=Integer.parseInt((String)request.getParameter("photoId"));
		int tagId=Integer.parseInt((String)request.getParameter("tagId"));
		tagDao.removeRelationshipTagPhoto(tagId, photoId);
		/*
		Photo photo=photoDao.findById(photoId);
		Tag tag=tagDao.findById(tagId);
		photo.getTags().remove(tag);
		tag.getPhotos().remove(photo);
		photoDao.merge(photo);
		tagDao.merge(tag);
		*/
		log.info("Tag "+tagId+" removed from photo "+photoId );
		CUtil.redirecter(request, response, "SViewPhoto?photoId="+photoId);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
